package com.ysw.io.读取单一文件并发入库;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.ysw.bean.User;
import com.ysw.mapper.UserMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author: yu_sw
 * @Description: com.ysw.io.读取单一文件并发入库
 * @date:2022/5/17 20:20
 */
@Component
public class ReadSingleFileUtil {
    final String filePath = "E:\\work\\spring_boot_learn\\experience\\src\\main\\java\\com\\ysw\\io\\data\\data_01.txt";

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private ThreadPoolTaskExecutor corePoll;

    public void readFile()  {
        FileInputStream fileInputStream = null;
        BufferedReader bufferedReader = null;
        InputStreamReader inputStreamReader = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
            bufferedReader = IOUtils.toBufferedReader(inputStreamReader);
            List<User> usersTemp = Lists.newArrayList();
            for (; ; ) {
                String line = bufferedReader.readLine();
                if (null == line) {
                    addTask(new ArrayList<>(usersTemp));
                    break;
                }
                User user = castStringToUserBean(line);
                if (null != user) {
                    usersTemp.add(user);
                }
                if (usersTemp.size() == 10000) {
                    addTask(new ArrayList<>(usersTemp));
                    usersTemp = Lists.newArrayList();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(bufferedReader,inputStreamReader,fileInputStream);
        }
    }

    /**
     * 添加异步任务
     * @param usersTemp
     */
    private void addTask(List<User> usersTemp) {
        List<List<User>> partition = Lists.partition(usersTemp, 1000);
        CountDownLatch countDownLatch = new CountDownLatch(partition.size());
        for (int i = 0; i < partition.size(); i++) {
            UserFileTask userFileTask = new UserFileTask(countDownLatch, partition.get(i), userMapper);
            corePoll.execute(userFileTask);
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
       /* ArrayList<Integer> integers = Lists.newArrayList(1, 2, 4);
        add(integers);
        System.out.println(integers);*/
        Integer m = 3;
        add(m);
        System.out.println(m);

    }

    static void add(List<Integer> integers) {
        integers.add(1);
        integers.add(2);
    }

    static void add(Integer i) {
        i++;
    }

    /**
     * 字符串转换为对象
     *
     * @param line
     * @return
     */
    public static User castStringToUserBean(String line) {
        if (StringUtils.isNotBlank(line)) {
            ArrayList<String> strings = Lists.newArrayList(Splitter.on("|").split(line));
            User user = new User();
            for (int i = 0; i < strings.size(); i++) {
                String field = StringUtils.isNotBlank(strings.get(i)) ? strings.get(i).trim() : "";
                switch (i) {
                    case 0:
                        user.setName(field);
                        break;
                    case 1:
                        user.setVersion(Integer.valueOf(field));
                        break;
                    case 2:
                        user.setAge(Integer.valueOf(field));
                        break;
                    case 3:
                        user.setCreateDate(LocalDateTime.parse(field));
                        break;
                    case 4:
                        user.setUpdateDate(new Date(field));
                        break;
                    default:
                        break;
                }
            }
            return user;
        }
        return null;
    }
}
